// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   CalenderRenderer.java

package com.jb.web.renderer;

import com.jb.faces.component.UIComponent;
import com.jb.web.bean.custom.WebCalendar;
import com.jb.web.context.WebContext;

// Referenced classes of package com.jb.web.renderer:
//            DefaultRenderer, RenderKitImpl, RendererImpl

public class CalenderRenderer extends DefaultRenderer {


    public static String buildURL(String originalURL, String key, 
                                  String value) {
        if (originalURL == null)
            originalURL = "";
        int i = originalURL.indexOf(key);
        if (i < 0) {
            originalURL = 
                    (new StringBuilder()).append(originalURL).append("&").append(key).append("=").append(value).toString();
        } else {
            int j = originalURL.indexOf("&", i + key.length());
            String rest = "";
            if (j > 0)
                rest = originalURL.substring(j);
            originalURL = 
                    (new StringBuilder()).append(originalURL.substring(0, i)).append(key).append("=").append(value).append(rest).toString();
        }
        return originalURL;
    }

    public StringBuffer render(WebContext pCtx, UIComponent itemToRender) {
        WebCalendar region = (WebCalendar)itemToRender;
        StringBuffer html = new StringBuffer("");
        String addURL = pCtx.formURL(pCtx.getRequestURL(), region);
        region.setDisplayMode(2);
        addURL = 
                buildURL(addURL, "month", (new StringBuilder()).append("").append(region.getMonth() + 
                                                                                  1).toString());
        String minusURL = pCtx.formURL(pCtx.getRequestURL(), region);
        minusURL = 
                buildURL(minusURL, "month", (new StringBuilder()).append("").append(region.getMonth() - 
                                                                                    1).toString());
        html.append("<TABLE ").append(addTagProperty("style", 
                                                     region.getStyle(pCtx), 
                                                     null)).append(addTagProperty("class", 
                                                                                  region.getStyleClass(), 
                                                                                  null)).append(addTagProperty("width", 
                                                                                                               region.getWidth(), 
                                                                                                               null));
        if (region.isShowHeader()) {
            html.append("<TR><TD ").append("colspan = \"7\" >");
            html.append("<TABLE cellSpacing=\"0px\" cellPadding=\"0px\" width=100%><TR>");
            html.append("<TD align=\"right\">");
            html.append("<a href=\"").append(minusURL).append("\"><img src=\"resources/images/windows/Back.gif\"></a>");
            html.append("</TD>");
            html.append("<TD style=\"text-align:center;\" width=\"100%\"><B>").append(getMonthString(pCtx, 
                                                                                                     region)).append("&nbsp;").append(getYearString(region)).append("</B></TD>");
            html.append("<TD align=\"left\">");
            html.append((new StringBuilder()).append("<a href=\"").append(addURL).append("\"><img src=\"resources/images/windows/Next.gif\"></a></TD>").toString());
            html.append("</TR></TABLE></TD></TR>");
        }
        String mon = pCtx.getResource("Mon");
        String tus = pCtx.getResource("Tues");
        String wed = pCtx.getResource("Wed");
        String thu = pCtx.getResource("Thu");
        String fri = pCtx.getResource("Fri");
        String sat = pCtx.getResource("Sat");
        String sun = pCtx.getResource("Sun");
        html.append("<TR >");
        html.append("<TD ").append(addTagProperty("class", 
                                                  region.getWeekStyle(), 
                                                  null)).append(" align=\"center\">").append(sun).append("</TD>");
        html.append("<TD ").append(addTagProperty("class", 
                                                  region.getWeekStyle(), 
                                                  null)).append(" align=\"center\">").append(mon).append("</TD>");
        html.append("<TD ").append(addTagProperty("class", 
                                                  region.getWeekStyle(), 
                                                  null)).append(" align=\"center\">").append(tus).append("</TD>");
        html.append("<TD ").append(addTagProperty("class", 
                                                  region.getWeekStyle(), 
                                                  null)).append(" align=\"center\">").append(wed).append("</TD>");
        html.append("<TD ").append(addTagProperty("class", 
                                                  region.getWeekStyle(), 
                                                  null)).append(" align=\"center\">").append(thu).append("</TD>");
        html.append("<TD ").append(addTagProperty("class", 
                                                  region.getWeekStyle(), 
                                                  null)).append(" align=\"center\">").append(fri).append("</TD>");
        html.append("<TD ").append(addTagProperty("class", 
                                                  region.getWeekStyle(), 
                                                  null)).append(" align=\"center\">").append(sat).append("</TD>");
        html.append("</TR>");
        RendererImpl cellRenderer = new DefaultCellRenderer();
        String cellRendererName = region.getCellRenderer();
        if (cellRendererName != null && cellRendererName.length() > 0)
            cellRenderer = RenderKitImpl.createRenderer(cellRendererName);
        String cellStyle = region.getCellStyle();
        for (int row = 0; row < 6; row++) {
            if (row > 0 && region.getDateAt(0, row) == -1)
                break;
            html.append("<TR >");
            for (int col = 0; col < 7; col++) {
                int date = region.getDateAt(col, row);
                region.setValue((new StringBuilder()).append("").append(date).toString());
                cellStyle = region.getCellStyle();
                if (col == 0 || col == 6)
                    cellStyle = region.getWeekendStyle();
                if (region.isDateSelected(date))
                    cellStyle = region.getSelectedStyle();
                String link = 
                    (new StringBuilder()).append("JB.post('").append(getCellLink(pCtx, 
                                                                                 region, 
                                                                                 date))
                    .append("','calendarP')").toString();
                html.append("<TD valign=\"top\" ")
                    .append(getEditScript(pCtx, region))
                    .append(addTagProperty("class", cellStyle, null))
                    .append(addTagProperty("onclick", link, null)).append(" >");
                html.append((new StringBuilder()).append("").append(cellRenderer.render(pCtx, 
                                                                                        region)).toString());
                html.append("</TD>");
            }

            html.append("</TR>");
        }

        html.append("</TABLE>");
        return html;
    }

    protected String getMonthString(WebContext pCtx, WebCalendar cal) {
        return pCtx.getResource(months[cal.getMonth()]);
    }

    protected String getYearString(WebCalendar cal) {
        return (new StringBuilder()).append("").append(cal.getYear()).toString();
    }

    public static String getCellLink(WebContext pCtx, WebCalendar cal, 
                                     int date) {
        return (new StringBuilder())
            .append(pCtx.formURL(pCtx.getRequestURL(), cal))
            .append("&jbCompId=").append(cal.getClientId())
            .append("&date=").append(date).append("&month=")
            .append(cal.getMonth()).append("&year=")
            .append(cal.getYear()).toString();
    }

    public static class DefaultCellRenderer extends DefaultRenderer {
            public String getCellLink(WebContext pCtx, WebCalendar cal, int date) {
                return pCtx.formURL(pCtx.getRequestURL(), cal) + "&jbCompId="+cal.getClientId()
                    +"&date=" + date + "&month=" + cal.getMonth() + "&year=" + cal.getYear();
            }

            public StringBuffer render(WebContext pCtx, UIComponent itemToRender) {
                StringBuffer html = new StringBuffer();
                WebCalendar region = (WebCalendar) itemToRender;

                String strDate = region.getValue();
                int date = Integer.parseInt(strDate);
              
                String link= "JB.post('"+getCellLink(pCtx, region, date)
                +"','calendarP')";
                
                if (date != -1) {
                    html.append("<a class=\"calLink\" ")
                        .append(addTagProperty("onclick", link, null))
                        .append(">")
                        .append(strDate).append("</a>");
                }

                return html;
            }
        }

    static String months[] = 
    { "January", "February", "March", "April", "May", "June", "July", "August", 
      "September", "October", "November", "December" };

}
